package com.ccp.dev.im.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.im.model.ImRoom;
import com.ccp.dev.im.model.ImUser;
import com.ccp.dev.im.server.constant.Constants;
import com.ccp.dev.im.service.ImRoomService;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.im.service.ImRoomUserService;
import com.ccp.dev.im.service.ImUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Set;


/**
 * 群 信息操作处理控制器
 *
 * @author y
 * @date 2020-02-17 11:39:36
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "群")
@RequestMapping("/im/imRoom")
public class ImRoomController extends SysBaseController {
    @Resource
    private ImRoomService imRoomService;

    @Resource
    private ImUserService imUserService;

    @Resource
    private ImRoomUserService imRoomUserService;

    /**
     * 功能描述: 跳转到群聊详情页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/groupInfo")
    @Action(description = "跳转到群聊详情页面", exectype = "页面跳转日志")
    public String groupInfo(HttpServletRequest request, ModelMap modelMap) {
        String roomId = RequestUtil.getString(request, "roomId");
        modelMap.put("roomId", roomId);
        return "/functionPage/groupfun/groupinfo";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: imRoom 保存的对象
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description = "创建群聊", detail = "创建群聊<#if success>成功<#else>失败</#if>")
    public Object save(@RequestBody ImRoom imRoom) {
        String id = imRoom.getId();
        int result = 0;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd", true);
            imRoom.setId(UUIDUtils.getUUIDFor32());
            //新增操作
            result = imRoomService.createRoom(imRoom);
        }
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return result > 0 ? getResultDataSuccess("operation.success", imRoom) : getResultDataError("operation.failure");
    }

    /**
     * 功能描述: 查询当前用户拥有的群ID集合
     *
     * @param: roomId 群ID
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping("/getCurrentOwnGroupId")
    @ResponseBody
    @Action(description = "查询当前用户拥有的群ID集合", detail = "查询当前用户拥有的群ID集合<#if success>成功<#else>失败</#if>")
    public Object getCurrentOwnGroupId() {
        ImUser imUser = imUserService.getByUserId(ContextUtil.getCurrentUser().getUserId());
        if (null == imUser) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        Set<String> roomIdSet = imRoomService.queryCurrentOwnGroupId(imUser.getId());
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("get.success", roomIdSet);
    }


    /**
     * 功能描述: 查询群详情
     *
     * @param: roomId 群ID
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description = "查询群详情", detail = "查询群详情<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("roomId") String roomId) {
        ImRoom imRoom = imRoomService.getOneById(roomId);
        if (null == imRoom) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        ImUser imUser = imUserService.getOneById(imRoom.getUserId());
        imRoom.setOwner(imUser.getUsername());
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("get.success", imRoom);
    }

    /**
     * 功能描述: 根据主键ID解散群
     *
     * @param: id 参数名称必须是"id"
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description = "解散群", exectype = "删除日志", detail = "解散群<#if success>成功<#else>失败</#if>")
    public Object delete(HttpServletRequest request) {
        String roomId = RequestUtil.getString(request, "roomId");
        if (StringUtil.isEmpty(roomId)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("operation.failure");
        }
        //判断当前用户是否是群主 是否可解散该群组
        String currentUserId = ContextUtil.getCurrentUserId();
        ImUser imUser = imUserService.getByUserId(currentUserId);
        if (BeanUtils.isNotEmpty(imUser)) {
            String currentImUserId = imUser.getId();
            ImRoom imRoomObj = imRoomService.getOneById(roomId);
            if (!imRoomObj.getUserId().equals(currentImUserId)) {
                return getResultDataError("noPermission.delRoom");
            }
        }
        int result = imRoomService.delRoom(roomId);
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return result > 0 ? getResultDataSuccess("operation.success") : getResultDataError("operation.failure");
    }


    /**
     * 功能描述: 修改群名称，群头像信息
     *
     * @param: imRoom 群对象
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping("/updateRoomInfo")
    @ResponseBody
    @Action(description = "修改群信息", exectype = "更新日志", detail = "修改群信息<#if success>成功<#else>失败</#if>")
    public Object updateRoomInfo(@RequestBody ImRoom imRoom) {
        try {
            ImRoom obj = imRoomService.getOneById(imRoom.getId());
            //查询群是否存在
            if (BeanUtils.isEmpty(obj)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "addDelRoomUsers.nullRoom");
            }
            //查询当前用户是否在这个群聊内
            boolean userIsExistCurRoom = imRoomUserService.qryCurrentUserExistOneRoom(imRoom.getId());
            if (!userIsExistCurRoom) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "updateRoom.noUser");
            }
            imRoomService.updateRoomInfo(imRoom);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            e.printStackTrace();
            return getResultDataError("operation.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("operation.success");
    }
}
